package com.xquant.pricing.calc.service.impl.otcOption.split;

import cn.hutool.core.util.NumberUtil;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.xquant.common.core.constant.DictConstant;
import com.xquant.pricing.calc.entity.CalcEngineParam4Simple;
import com.xquant.pricing.calc.entity.simple.*;
import com.xquant.common.engine.xPP.request.CalcEngineParam;
import com.xquant.common.engine.xPP.request.CalcPricingParam;
import com.xquant.common.engine.xPP.request.Instrument;
import com.xquant.pricing.sysconfig.service.ISystemConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.List;


/**
 * 功能说明：单鲨期权拆分
 * 编写作者：botao.yu
 * 开发日期：2021/11/12 13:47
 * 修改记录：修改日期   修改人    修改内容
 */
@Component
public class CalcCommonService4OtcOptionSplit4Shark implements CalcCommonService4OtcOptionSplit {

    @Autowired
    private ISystemConfigService systemConfigService;
    @Override
    public CalcEngineParam split(CalcEngineParam4Simple sourceCalcEngineParam) {
        {
            CalcEngineParam engineParam = new CalcEngineParam();
            engineParam.setPriority("h");
            engineParam.setLocalLog("1");
            Instrument instrument = new Instrument();
            instrument.setProductType(DictConstant.OPTION_TYPE.VANILLA.getCode());
            Record record = sourceCalcEngineParam.getRecord();
            BaseInfo baseInfo = record.getBaseInfo();
            OptionTrade OptionTrade = record.getOptionTrade();
            String productType = null;
            List<PremiumItem> premiumList = null;
            if (OptionTrade != null) {
                premiumList = OptionTrade.getPremium();
            }
            List<OptionLeg> legDataList = OptionTrade.getOptionLegs();
            LegData legData = null;
            if (legDataList.size() > 0) {
                productType = legDataList.get(0).getProductType();
                legData = legDataList.get(0).getLegData();
            }
            if (legData != null) {
                int i = 0;
                BigDecimal annualRatio = getAnnualRatio(getCalcDate(sourceCalcEngineParam), legData.getEffectiveDate(), legData.getTerminationDate(), transferBasic(legData.getDayCounter(), legData.getIfAnnual()));
                JSONObject termData = new JSONObject();
                JSONArray legsJsonArray = new JSONArray();
                JSONArray custLegsArray = new JSONArray();
                termData.put("notional", legData.getNotional());
                termData.put("notionalCurrency", "CNY");
                termData.put("term", legData.getTerm());
                termData.put("settlementType", "C");
                termData.put("settlementCurrency", "CNY");
                termData.put("effectiveDate", legData.getEffectiveDate());
                termData.put("effectiveDateConv", "Unadjusted");
                termData.put("terminationDate", legData.getTerminationDate());
                termData.put("terminationDateConv", "Unadjusted");
                termData.put("terminationDateCalendar", "CHINA_EX");
                legData.setUnderlyerId(getInstructionId((String) legData.getUnderlyerId()));
                String[] underLyerArray = {legData.getUnderlyerId()};
                termData.put("underlyerIds", underLyerArray);
                termData.put("calendars", new String[]{"CHINA_EX"});

                if (DictConstant.OBSERVE_TYPE.AMERICAN.equals(legData.getObserveType())) {
                    //todo 开关  变成单障碍 knockout  向上
                    if (systemConfigService.selectConfigByKey("environment").equals("jypoc")) {

                        return generateInstrument(sourceCalcEngineParam);
                    }
                    JSONObject custLegsJSON = new JSONObject();
                    //双敲出障碍 +双敲出触碰
                    //记录腿号及方向
                    JSONObject legsJSON = new JSONObject();
                    legsJSON.put("legId", "leg01");
                    legsJSON.put("payDirection", DictConstant.IR_DIRECT.SELL.equals(legData.getTrdType()) ? DictConstant.DIRECT.SELL : DictConstant.DIRECT.BUY);
                    legsJsonArray.add(legsJSON);
                    termData.put("legs", legsJsonArray);
                    //拼装customeLeg
                    custLegsJSON.put("participationRate", (String) legData.getParticipationRate());
                    //看涨是向上敲出  看跌是向下敲出 在系统中传入的值都为KnockOut
                    if (DictConstant.OPTION_DIRECT.CALL.equals(legData.getOptionType())) {
                        custLegsJSON.put("productType", DictConstant.KNOCK_TYPE.UP_AND_OUT);
                    } else {
                        custLegsJSON.put("productType", DictConstant.KNOCK_TYPE.UP_AND_OUT);
                    }
                    custLegsJSON.put("optionType", (String) legData.getOptionType());
                    custLegsJSON.put("strikePercentage", (String) legData.getStrikeRate());
                    custLegsJSON.put("numberOfOptions", record.getNumberOfOptions());
                    custLegsJSON.put("optionEntitlement", Double.parseDouble(legData.getNotional().toString()) / Double.parseDouble(legData.getSpotPrice().toString()));//标的份数=金额/标的初始价格
                    custLegsJSON.put("moneynessOption", legData.getIfAnnual());//是否年化
                    custLegsJSON.put("daycounter", "Actual/365 (Fixed)");//计息基准
                    custLegsJSON.put("fixingRounding", "-1");//定盘精度
                    custLegsJSON.put("settlementDateConv", "Unadjusted");//结算日调整
                    custLegsJSON.put("settlementDateCalendar", "CHINA_EX");//结算日日历
                    custLegsJSON.put("settlementDateOffset", (String) legData.getSettlementDateOffset() + "D");//结算日偏移
                    custLegsJSON.put("legId", "leg01");//结算日日历
                    JSONObject exerciseJSON = new JSONObject();
                    exerciseJSON.put("type", "European");//默认欧式
                    exerciseJSON.put("conv", "Unadjusted");//行权日调整方式
                    exerciseJSON.put("calendar", "CHINA_EX");//行权日日历
                    String[] exeDate = {legData.getTerminationDate()};//行权日
                    exerciseJSON.put("dates", exeDate);
                    custLegsJSON.put("exercise", exerciseJSON);
                    //障碍结构
                    JSONObject knockJSON = new JSONObject();
                    knockJSON.put("paymentDateOffset", (String) legData.getSettlementDateOffset() + "D");//结算日偏移
                    knockJSON.put("paymentDateConv", "Unadjusted");//行权日调整方式
                    knockJSON.put("paymentDateCalendar", "CHINA_EX");//行权日日历
//                    if ("1".equals(legData.getIfAnnual())) {
//                        knockJSON.put("paymentAmount", legData.getRebateRate());//支付水平
//                    } else {
                        knockJSON.put("paymentAmountPecentage", legData.getRebateRate());//支付水平 取补偿收益率
//                    }
//            exerciseJSON.put("paymentAmount",legData);//支付金额
                    JSONObject triggerJSON = new JSONObject();
                    triggerJSON.put("type", "Greater");
                    triggerJSON.put("levelPercentage", (String) legData.getStrikeRate());
                    knockJSON.put("trigger", triggerJSON);
                    knockJSON.put("participationRate", (String) legData.getParticipationRate());
                    knockJSON.put("payStyle", DictConstant.IR_PAY_STYLE.TRRIGER_PAY.equals(legData.getPayStyle()) ? DictConstant.PAY_STYLE.TRRIGER_PAY : DictConstant.PAY_STYLE.EXPIR_PAY);
                    //观察结构
                    JSONObject observeJSON = new JSONObject();
                    observeJSON.put("startDate", legData.getEffectiveDate());
                    observeJSON.put("startDateConv", "Following");
                    observeJSON.put("isIncludeStartDate", "1");
                    observeJSON.put("endDate", legData.getTerminationDate());
                    observeJSON.put("isIncludeEndDate", "1");
                    observeJSON.put("endDateConv", "Unadjusted");
                    observeJSON.put("calendar", "CHINA_EX");
                    //连续观察和每日观察的处理  TODO
                    if (legData.getScheduleType().equals(DictConstant.SCHEDULE_TYPE.CONT_OBS)) {
                        observeJSON.put("frequency", "0D");
                    } else {
                        observeJSON.put("frequency", "1D");
                    }
                    knockJSON.put("observeDates", observeJSON);
                    custLegsJSON.put("knock", knockJSON);
                    //标的结构
                    JSONObject underLyerJSON = new JSONObject();
                    JSONArray underLyerJSONArray = new JSONArray();
                    underLyerJSON.put("instrumentId", legData.getUnderlyerId());
                    underLyerJSON.put("quoteType", "");
                    underLyerJSON.put("spotPrice", legData.getSpotPrice());//行权价格
                    underLyerJSON.put("basketWeight", "1"); //权重
                    underLyerJSON.put("fixingDate", legData.getFixingDate());//定盘日
                    underLyerJSON.put("fixingDateConv", "Preceding");//定盘日调整
                    underLyerJSON.put("fixingDateCalendar", "CHINA_EX");//定盘日日历
                    underLyerJSONArray.add(underLyerJSON);
                    custLegsJSON.put("underlyers", underLyerJSONArray);
                    custLegsArray.add(custLegsJSON);
                    i = 2;
                } else {
                    //欧式拆分：2条香草期权(价差)+二元  拼装legs和customLegs
                    JSONObject legsJSON = new JSONObject();
                    legsJSON.put("legId", "leg01");
                    legsJSON.put("payDirection", DictConstant.IR_DIRECT.SELL.equals(legData.getTrdType()) ? DictConstant.DIRECT.SELL : DictConstant.DIRECT.BUY);
                    legsJsonArray.add(legsJSON);
                    termData.put("legs", legsJsonArray);
                    //拼装customeLeg
                    JSONObject custLegsJSON = new JSONObject();
                    custLegsJSON.put("participationRate", (String) legData.getParticipationRate());
                    custLegsJSON.put("productType", DictConstant.OPTION_TYPE.VANILLA.getCode());
                    custLegsJSON.put("optionType", (String) legData.getOptionType());
                    custLegsJSON.put("strikePercentage", (String) legData.getStrikeRate());
                    custLegsJSON.put("numberOfOptions", "1");
                    custLegsJSON.put("optionEntitlement", Double.parseDouble(legData.getNotional().toString()) / Double.parseDouble(legData.getSpotPrice().toString()));//标的份数=金额/标的初始价格
                    custLegsJSON.put("moneynessOption", legData.getIfAnnual());//是否年化
                    custLegsJSON.put("daycounter", "Actual/365 (Fixed)");//计息基准
                    custLegsJSON.put("fixingRounding", "-1");//定盘精度
                    custLegsJSON.put("settlementDateConv", "Unadjusted");//结算日调整
                    custLegsJSON.put("settlementDateCalendar", "CHINA_EX");//结算日日历
                    custLegsJSON.put("settlementDateOffset", (String) legData.getSettlementDateOffset() + "D");//结算日偏移
                    custLegsJSON.put("legId", "leg01");//结算日日历
                    JSONObject exerciseJSON = new JSONObject();
                    exerciseJSON.put("type", "European");//默认欧式
                    exerciseJSON.put("conv", "Unadjusted");//行权日调整方式
                    exerciseJSON.put("calendar", "CHINA_EX");//行权日日历
                    String[] exeDate = {legData.getTerminationDate()};//行权日
                    exerciseJSON.put("dates", exeDate);
                    custLegsJSON.put("exercise", exerciseJSON);
                    JSONObject underLyerJSON = new JSONObject();
                    JSONArray underLyerJSONArray = new JSONArray();
                    underLyerJSON.put("instrumentId", legData.getUnderlyerId());
                    underLyerJSON.put("quoteType", "");
                    underLyerJSON.put("spotPrice", legData.getSpotPrice());//行权价格
                    underLyerJSON.put("basketWeight", "1"); //权重
                    underLyerJSON.put("fixingDate", legData.getFixingDate());//定盘日
                    underLyerJSON.put("fixingDateConv", "Preceding");//定盘日调整
                    underLyerJSON.put("fixingDateCalendar", "CHINA_EX");//定盘日日历
                    underLyerJSONArray.add(underLyerJSON);
                    custLegsJSON.put("underlyers", underLyerJSONArray);
                    custLegsArray.add(custLegsJSON);
                    //第二条香草
                    JSONObject legsJSON2 = new JSONObject();
                    legsJSON2.put("legId", "leg02");
                    legsJSON2.put("payDirection", DictConstant.IR_DIRECT.SELL.equals(legData.getTrdType()) ? DictConstant.DIRECT.BUY : DictConstant.DIRECT.SELL);
                    legsJsonArray.add(legsJSON2);
                    //拼装customeLeg
                    JSONObject custLegsJSON2 = JSON.parseObject(JSON.toJSONString(custLegsJSON));
                    custLegsJSON2.put("legId", "leg02");
                    custLegsJSON2.put("strikePercentage", (String) legData.getTriggerRate());
                    custLegsJSON2.put("moneynessOption", legData.getIfAnnual());//是否年化
                    custLegsJSON2.put("daycounter", "Actual/365 (Fixed)");//计息基准
                    custLegsArray.add(custLegsJSON2);
                    //二元结构
                    JSONObject custLegsJSON3 = new JSONObject();
                    custLegsJSON3.put("participationRate", (String) legData.getParticipationRate());
                    custLegsJSON3.put("productType", DictConstant.OPTION_TYPE.DIGITAL.getCode());
                    custLegsJSON3.put("optionType", (String) legData.getOptionType());
                    custLegsJSON3.put("strikePercentage", (String) legData.getTriggerRate());
                    custLegsJSON3.put("numberOfOptions", "1");
                    custLegsJSON3.put("optionEntitlement", Double.parseDouble(legData.getNotional().toString()) / Double.parseDouble(legData.getSpotPrice().toString()));//标的份数=金额/标的初始价格
                    custLegsJSON3.put("moneynessOption", legData.getIfAnnual());//是否年化
                    custLegsJSON3.put("daycounter", "Actual/365 (Fixed)");//计息基准
                    custLegsJSON3.put("fixingRounding", "-1");//定盘精度
                    custLegsJSON3.put("settlementDateConv", "Unadjusted");//结算日调整
                    custLegsJSON3.put("settlementDateCalendar", "CHINA_EX");//结算日日历
                    custLegsJSON3.put("settlementDateOffset", (String) legData.getSettlementDateOffset() + "D");//结算日偏移
                    custLegsJSON3.put("legId", "leg03");//结算日日历
                    JSONObject exerciseJSON3 = new JSONObject();
                    exerciseJSON3.put("type", "European");//默认欧式
                    exerciseJSON3.put("conv", "Unadjusted");//行权日调整方式
                    exerciseJSON3.put("calendar", "CHINA_EX");//行权日日历
                    String[] exeDate3 = {legData.getTerminationDate()};//行权日
                    exerciseJSON3.put("dates", exeDate3);
                    custLegsJSON3.put("exercise", exerciseJSON3);
                    //二元结构
                    JSONObject digitalJSON = new JSONObject();
                    digitalJSON.put("paymentDateOffset", (String) legData.getSettlementDateOffset() + "D");//结算日偏移
                    digitalJSON.put("paymentDateConv", "Unadjusted");//行权日调整方式
                    digitalJSON.put("paymentDateCalendar", "CHINA_EX");//行权日日历
                    //期权参与率*（障碍价-行权价） -补偿收益率*补偿参与了+最低收益率
                    BigDecimal payPrice1 = NumberUtil.mul(legData.getParticipationRate(), NumberUtil.sub(legData.getTriggerRate(), legData.getStrikeRate()).toString());
                    BigDecimal payPrice2 = NumberUtil.mul(legData.getRebateRate(), legData.getRebateParticipationRate());
                    BigDecimal payPrice3 = NumberUtil.add(NumberUtil.sub(payPrice1, payPrice2).toString(), legData.getMinYield());
                    if ("1".equals(legData.getIfAnnual())) {
                        digitalJSON.put("paymentAmount", payPrice3.multiply(BigDecimal.valueOf(Integer.valueOf(legData.getNotional()))).multiply(annualRatio));//支付水平 = 名义本金x年化系数xpyPrice
                    } else {
                        digitalJSON.put("paymentAmountPecentage", payPrice3);
                    }
//            exerciseJSON.put("paymentAmount",legData);//支付金额
                    JSONObject triggerJSON = new JSONObject();
                    triggerJSON.put("type", "Greater");
                    triggerJSON.put("levelPercentage", (String) legData.getTriggerRate());
                    digitalJSON.put("trigger", triggerJSON);
                    custLegsJSON3.put("digital", digitalJSON);
                    //标的结构
                    JSONObject underLyerJSON3 = new JSONObject();
                    JSONArray underLyerJSONArray3 = new JSONArray();
                    underLyerJSON3.put("instrumentId", legData.getUnderlyerId());
                    underLyerJSON3.put("quoteType", "");
                    underLyerJSON3.put("spotPrice", legData.getSpotPrice());//行权价格
                    underLyerJSON3.put("basketWeight", "1"); //权重
                    underLyerJSON3.put("fixingDate", legData.getFixingDate());//定盘日
                    underLyerJSON3.put("fixingDateConv", "Preceding");//定盘日调整
                    underLyerJSON3.put("fixingDateCalendar", "CHINA_EX");//定盘日日历
                    underLyerJSONArray3.add(underLyerJSON3);
                    custLegsJSON3.put("underlyers", underLyerJSONArray3);
                    custLegsArray.add(custLegsJSON3);
                    JSONObject legsJSON3 = new JSONObject();
                    legsJSON3.put("legId", "leg03");
                    legsJSON3.put("payDirection", DictConstant.IR_DIRECT.SELL.equals(legData.getTrdType()) ? DictConstant.DIRECT.BUY : DictConstant.DIRECT.SELL);
                    legsJsonArray.add(legsJSON3);
                    i = 4;
                }
                //期权费生成
                for (PremiumItem p : premiumList) {
                    JSONObject feeObj = getIRSteamObj(legData, p.getPaymentAmountRate());
                    feeObj.put("legId", "leg0" + i);
                    //根据期权费生成现金借贷
                    JSONObject legsJson = new JSONObject();
                    legsJson.put("legId", "leg0" + i);
                    legsJson.put("payDirection", DictConstant.DIRECT.SELL);
                    legsJsonArray.add(legsJson);
                    custLegsArray.add(feeObj);
                    i++;
                }
                //最低收益率增加现金借贷
                if (legData.getMinYield() != null && Double.parseDouble(legData.getMinYield()) > 0) {
                    JSONObject minObj = getIRSteamObj(legData, legData.getRebateRate());
                    minObj.put("legId", "leg0" + i);
                    JSONObject legsJson = new JSONObject();
                    legsJson.put("legId", "leg0" + i);
                    legsJson.put("payDirection", DictConstant.IR_DIRECT.SELL.equals(legData.getTrdType()) ? DictConstant.DIRECT.SELL : DictConstant.DIRECT.BUY);
                    legsJsonArray.add(legsJson);
                    custLegsArray.add(minObj);
                }
                termData.put("customLegs", custLegsArray);
                instrument.setTermsheet(termData);
                instrument.setProductType(DictConstant.PRODUCT_TYPE.STRATEGY);
                engineParam.setInstrument(instrument);
                CalcPricingParam calPriceParamObj = new CalcPricingParam();
                calPriceParamObj.setValueDate(getCalcDate(sourceCalcEngineParam));
                JSON.toJSONString(calPriceParamObj);
                engineParam.setCalcPricingParam(calPriceParamObj);
                return engineParam;
            }
            return null;
        }
    }

    private CalcEngineParam generateInstrument(CalcEngineParam4Simple sourceCalcEngineParam) {
        CalcEngineParam engineParam = new CalcEngineParam();
        engineParam.setPriority("h");
        engineParam.setLocalLog("1");
        Instrument instrument = new Instrument();
        instrument.setProductType(DictConstant.OPTION_TYPE.KnockOut.getCode());
        Record record = sourceCalcEngineParam.getRecord();
        BaseInfo baseInfo = record.getBaseInfo();
        OptionTrade OptionTrade = record.getOptionTrade();
        String productType = null;
        List<PremiumItem> premiumList = null;
        if (OptionTrade != null) {
            premiumList = OptionTrade.getPremium();
        }
        List<OptionLeg> legDataList = OptionTrade.getOptionLegs();
        LegData legData = null;
        if (legDataList.size() > 0) {
            productType = legDataList.get(0).getProductType();
            legData = legDataList.get(0).getLegData();
        }


        JSONObject termData = new JSONObject();
        JSONArray legsJsonArray = new JSONArray();
        JSONArray custLegsArray = new JSONArray();
        termData.put("notional", legData.getNotional());
        termData.put("notionalCurrency", "CNY");
        termData.put("term", legData.getTerm());
        termData.put("settlementType", "C");
        termData.put("settlementCurrency", "CNY");
        termData.put("effectiveDate", legData.getEffectiveDate());
        termData.put("effectiveDateConv", "Unadjusted");
        termData.put("terminationDate", legData.getTerminationDate());
        termData.put("terminationDateConv", "Unadjusted");
        termData.put("terminationDateCalendar", "CHINA_EX");
        legData.setUnderlyerId(getInstructionId((String) legData.getUnderlyerId()));
        String[] underLyerArray = {legData.getUnderlyerId()};
        termData.put("underlyerIds", underLyerArray);
        termData.put("calendars", new String[]{"CHINA_EX"});

        JSONObject legsJSON1 = new JSONObject();
        legsJSON1.put("legId", "leg01");
        legsJSON1.put("payDirection", DictConstant.IR_DIRECT.SELL.equals(legData.getTrdType()) ? DictConstant.DIRECT.SELL : DictConstant.DIRECT.BUY);
        legsJsonArray.add(legsJSON1);


        //拼装customeLegs
        JSONArray customLegs = new JSONArray();

        JSONObject customleg01 = new JSONObject();
        customleg01.put("legId", "leg01");
        customleg01.put("productType", "KnockOut");
        customleg01.put("optionType", legData.getOptionType());
        customleg01.put("strikePercentage", (String) legData.getStrikeRate());
        customleg01.put("numberOfOptions", record.getNumberOfOptions());
        customleg01.put("optionEntitlement", Double.parseDouble(legData.getNotional().toString()) / Double.parseDouble(legData.getSpotPrice().toString()));//标的份数=金额/标的初始价格
        customleg01.put("moneynessOption", legData.getIfAnnual());//是否年化
        customleg01.put("daycounter", legData.getDayCounter());//计息基准
        customleg01.put("fixingRounding", "-1");//定盘精度
        customleg01.put("settlementDateConv", "Following");//结算日调整
        customleg01.put("settlementDateCalendar", "CHINA_EX");//结算日日历
//        customleg01.put("settlementDateOffset", (String) legData.getSettlementDateOffset() + "D");//结算日偏移
        customleg01.put("settlementDateOffset", "0D");//结算日偏移
        customleg01.put("legId", "leg01");//结算日日历
        JSONObject exerciseJSON = new JSONObject();
        exerciseJSON.put("type", "European");//默认欧式
        exerciseJSON.put("conv", "Unadjusted");//行权日调整方式
        exerciseJSON.put("calendar", "CHINA_EX");//行权日日历
        String[] exeDate = {legData.getTerminationDate()};//行权日
        exerciseJSON.put("dates", exeDate);
        customleg01.put("exercise", exerciseJSON);
        //障碍结构
        JSONObject knockJSON = new JSONObject();
//        knockJSON.put("paymentDateOffset", (String) legData.getSettlementDateOffset() + "D");//结算日偏移
        knockJSON.put("paymentDateOffset", "0D");//结算日偏移
        knockJSON.put("paymentDateConv", "Unadjusted");//行权日调整方式
        knockJSON.put("paymentDateCalendar", "CHINA_EX");//行权日日历
//        if ("1".equals(legData.getIfAnnual())) {
//            knockJSON.put("paymentAmount", legData.getRebatePrice());//支付水平
//        } else {
            knockJSON.put("paymentAmountPecentage", legData.getRebateRate());//支付水平 取补偿收益率
//        }
//            exerciseJSON.put("paymentAmount",legData);//支付金额
        JSONObject triggerJSON = new JSONObject();
        if (legData.getOptionType().equals("Call")) {
            triggerJSON.put("type", "EqualOrGreater");
        } else {
            triggerJSON.put("type", "EqualOrLess");
        }
        triggerJSON.put("levelPercentage", (String) legData.getTriggerRate());
        knockJSON.put("trigger", triggerJSON);
        knockJSON.put("participationRate", (String) legData.getParticipationRate());
        knockJSON.put("payStyle", DictConstant.IR_PAY_STYLE.TRRIGER_PAY.equals(legData.getPayStyle()) ? DictConstant.PAY_STYLE.TRRIGER_PAY : DictConstant.PAY_STYLE.EXPIR_PAY);
        //观察结构
        JSONObject observeJSON = new JSONObject();
        observeJSON.put("startDate", legData.getEffectiveDate());
        observeJSON.put("startDateConv", "Following");
        observeJSON.put("isIncludeStartDate", "1");
        observeJSON.put("endDate", legData.getTerminationDate());
        observeJSON.put("isIncludeEndDate", "1");
        observeJSON.put("endDateConv", "Unadjusted");
        observeJSON.put("calendar", "CHINA_EX");
        //连续观察和每日观察的处理  TODO
        if (legData.getScheduleType().equals(DictConstant.SCHEDULE_TYPE.CONT_OBS)) {
            observeJSON.put("frequency", "0D");
        } else {
            observeJSON.put("frequency", "1D");
        }
        knockJSON.put("observeDates", observeJSON);
        customleg01.put("knock", knockJSON);
        //标的结构
        JSONObject underLyerJSON = new JSONObject();
        JSONArray underLyerJSONArray = new JSONArray();
        underLyerJSON.put("instrumentId", legData.getUnderlyerId());
        underLyerJSON.put("quoteType", "");
        underLyerJSON.put("spotPrice", legData.getSpotPrice());//行权价格
        underLyerJSON.put("basketWeight", "1"); //权重
        underLyerJSON.put("fixingDate", legData.getFixingDate());//定盘日
        underLyerJSON.put("fixingDateConv", "Preceding");//定盘日调整
        underLyerJSON.put("fixingDateCalendar", "CHINA_EX");//定盘日日历
        underLyerJSONArray.add(underLyerJSON);
        customleg01.put("underlyers", underLyerJSONArray);
        custLegsArray.add(customleg01);
        termData.put("legs", legsJsonArray);
        //NOTOUCH 结构
        /*JSONObject touchlegsJSON = new JSONObject();
        touchlegsJSON.put("legId", "leg02");
        touchlegsJSON.put("payDirection", DictConstant.IR_DIRECT.SELL.equals(legData.getTrdType()) ? DictConstant.DIRECT.SELL : DictConstant.DIRECT.BUY);
        legsJsonArray.add(touchlegsJSON);
        termData.put("legs", legsJsonArray);
        JSONObject touchObject = new JSONObject();
        JSONArray touchunder = new JSONArray();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("quoteType", "");
        jsonObject.put("instrumentId", legData.getUnderlyerId());
        jsonObject.put("spotPrice", legData.getSpotPrice());//行权价格
        jsonObject.put("basketWeight", "1"); //权重
        jsonObject.put("fixingDate", legData.getFixingDate());//定盘日
        jsonObject.put("fixingDateConv", "Preceding");//定盘日调整
        jsonObject.put("fixingDateCalendar", "CHINA_EX");//定盘日日历
        touchunder.add(jsonObject);
        touchObject.put("underlyers", touchunder);
        //拼装customeLeg
        touchObject.put("participationRate", (String) legData.getParticipationRate());
        //看涨是向上敲出  看跌是向下敲出 在系统中传入的值都为KnockOut
        touchObject.put("productType", "NoTouch");
        touchObject.put("legId", "leg02");
        touchObject.put("numberOfOptions", record.getNumberOfOptions());
        touchObject.put("optionEntitlement", Double.parseDouble(legData.getNotional().toString()) / Double.parseDouble(legData.getSpotPrice().toString()));//标的份数=金额/标的初始价格
        touchObject.put("moneynessOption", legData.getIfAnnual());//是否年化
        touchObject.put("daycounter", "Actual/365 (Fixed)");//计息基准
        touchObject.put("fixingRounding", "-1");//定盘精度
        JSONObject touch = new JSONObject();
        JSONObject trigger = new JSONObject();
        if (legData.getOptionType().equals("Call")){
            trigger.put("type", "EqualOrGreater");
        }else {
            trigger.put("type", "EqualOrLess");
        }
        trigger.put("levelPercentage", (String) legData.getTriggerRate());
        touch.put("trigger",trigger);
        touchObject.put("touch",touch);
        JSONObject observeDates = new JSONObject();
        observeDates.put("startDate",legData.getEffectiveDate());
        observeDates.put("startDateConv","NONE");
        observeDates.put("endDate",legData.getTerminationDate());
        observeDates.put("endDateConv","NONE");
        observeDates.put("frequency","1D");
        observeDates.put("calendar","CHINA_EX");
//                    observeDates.put("discreteDates",);
        touch.put("observeDates",observeDates);
        touch.put("observeType","");
        touch.put("participationRate", (String) legData.getParticipationRate());
        touch.put("paymentAmountPecentage", legData.getMinYield());
        touch.put("paymentDateCalendar", "CHINA_EX");
        touch.put("paymentDateConv", "NONE");
        touch.put("paymentDateOffset", (String) legData.getSettlementDateOffset() + "D");

        custLegsArray.add(touchObject);*/
        termData.put("customLegs", custLegsArray);
        instrument.setTermsheet(termData);
        engineParam.setInstrument(instrument);
        return engineParam;
    }

    @Override
    public CalcEngineParam split(CalcEngineParam sourceCalcEngineParam) {
        return null;
    }

    @Override
    public String getSplitCode() {
        return DictConstant.OPTION_TYPE.SHARK.getCode();
    }


}
